package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
    /**
     * 分页条件查询
     * 接收多个普通参数(?key=value&key2=value2)，不要加@RequestParam注解
     * @RequestBody(json{key:value,key2:value2...})
     * */
    @GetMapping
    public Result page(EmpQueryParam empQueryParam)
    {
        log.info("查询参数：{}",empQueryParam);
        PageResult<Emp> pageResult=empService.page(empQueryParam);
        return Result.success(pageResult);
    }
    @PostMapping
    public Result add(@RequestBody Emp emp)
    {
        log.info("新增员工：{}",emp);
        empService.add(emp);
        return Result.success();
    }
    @DeleteMapping
    public Result deleteByIds(@RequestParam("ids") List<Integer> ids)
    {
        log.info("根据员工删除"+ids);
        empService.deleteByIds(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable(value = "id")Integer id)
    {
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }
    @PutMapping
    public Result update(@RequestBody Emp emp)
    {
        log.info("修改员工：{}",emp);
        empService.update(emp);
        return Result.success();
    }

}
